﻿using System.Collections;
using System.Collections.Generic;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;

namespace EOSCRM.Web.Common
{
    public sealed class ReportDocument : CrystalDecisions.CrystalReports.Engine.ReportDocument
    {
        public bool Loaded { get; set; }
        public string UniqueId { get; set; }

        //public ReportDocument(string reportPath, string uniqueId)
        //{
        //    var _reportPath = "";
        //    try
        //    {
        //        UniqueId = SessionKey.SESSION_PREFIX + uniqueId;
        //        if (!string.IsNullOrEmpty(uniqueId))
        //            _reportPath = SessionManager.SessionValue(UniqueId + SessionKey.RP_SUFFIX_REPORTPATH) as string;
        //    }
        //    catch { }

        //    try
        //    {
        //        Load(reportPath);
        //        if (_reportPath == reportPath) 
        //        {
        //            SetDataSource(SessionManager.SessionValue(UniqueId) as DataTable);
        //            loadChangeTEXT();
        //            Loaded = true;
        //        }
        //        else
        //        {
        //            SessionManager.AddSession(UniqueId + SessionKey.RP_SUFFIX_REPORTPATH, reportPath);
        //            SessionManager.RemoveSession(UniqueId);
        //            SessionManager.RemoveSession(UniqueId + SessionKey.RP_SUFFIX_CHANGETEXT);
        //            Loaded = false;
        //        }
        //    }
        //    catch { }
        //}

        //public override void SetDataSource(DataTable dataTable)
        //{
        //    SessionManager.AddSession(UniqueId, dataTable);

        //    base.SetDataSource(dataTable);
        //    Loaded = !string.IsNullOrEmpty(FileName);
        //}

        //public override void SetDataSource(IEnumerable enumerable)
        //{
        //    SessionManager.AddSession(UniqueId, enumerable);

        //    base.SetDataSource(enumerable);
        //    Loaded = !string.IsNullOrEmpty(FileName);
        //}


        #region Functions for report
        public class ChangeTextObject
        {
            public string ID { get; set; }
            public string Text { get; set; }
            public int Width { get; set; }
        }

        void saveChangeTEXT(string sID, string idText, int newWIDTHinPIXEL)
        {
            List<ChangeTextObject> saveValues = null;
            try { saveValues = SessionManager.SessionValue(UniqueId + SessionKey.RP_SUFFIX_CHANGETEXT) as List<ChangeTextObject>; }
            catch { }
            if (saveValues == null) saveValues = new List<ChangeTextObject>();
            var saveValue = saveValues.Find(f => f.ID == sID);
            if (saveValue != null)
            {
                saveValue.Text = idText;
                saveValue.Width = newWIDTHinPIXEL;
            }
            else saveValues.Add(new ChangeTextObject { ID = sID, Text = idText, Width = newWIDTHinPIXEL });
            SessionManager.AddSession(UniqueId + SessionKey.RP_SUFFIX_CHANGETEXT, saveValues);
        }
        void loadChangeTEXT()
        {
            List<ChangeTextObject> saveValues = null;
            try { saveValues = SessionManager.SessionValue(UniqueId + SessionKey.RP_SUFFIX_CHANGETEXT) as List<ChangeTextObject>; }
            catch { }
            if (saveValues != null)
                foreach (var t in saveValues)
                    if (t.Width > 0) changeTEXT(t.ID, t.Text, t.Width, false);
                    else changeTEXT(t.ID, t.Text, false);
        }
        public void changeTEXT(string sID, string idText) { changeTEXT(sID, idText, true); }
        void changeTEXT(string sID, string idText,bool saveTEXT)
        {
            try
            {
                var textObject = ReportDefinition.ReportObjects[sID] as TextObject;
                if (textObject != null)
                    textObject.Text = idText;
                if (saveTEXT) saveChangeTEXT(sID, idText, 0);
            }
            catch { }
        }
        public void changeTEXT(string sID, string idText, int newWIDTHinPIXEL) { changeTEXT(sID, idText, newWIDTHinPIXEL, true); }
        void changeTEXT(string sID, string idText, int newWIDTHinPIXEL, bool saveTEXT)
        {
            try
            {
                var objTEXT = ReportDefinition.ReportObjects[sID] as TextObject;
                if (objTEXT != null)
                {
                    objTEXT.Text = idText;
                    objTEXT.Width = newWIDTHinPIXEL * 15;
                }
                if (saveTEXT) saveChangeTEXT(sID, idText, newWIDTHinPIXEL);
            }
            catch { }
        }
        #endregion
    }
}
